package com.ruoyi.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.CustomerAddr;
import com.ruoyi.order.service.ICustomerAddrService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户地址管理Controller
 * 
 * @author lzp
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/system/addr")
public class CustomerAddrController extends BaseController
{
    @Autowired
    private ICustomerAddrService customerAddrService;

    /**
     * 查询客户地址管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:addr:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerAddr customerAddr)
    {
        startPage();
        List<CustomerAddr> list = customerAddrService.selectCustomerAddrList(customerAddr);
        return getDataTable(list);
    }

    /**
     * 导出客户地址管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:addr:export')")
    @Log(title = "客户地址管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerAddr customerAddr)
    {
        List<CustomerAddr> list = customerAddrService.selectCustomerAddrList(customerAddr);
        ExcelUtil<CustomerAddr> util = new ExcelUtil<CustomerAddr>(CustomerAddr.class);
        util.exportExcel(response, list, "客户地址管理数据");
    }

    /**
     * 获取客户地址管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:addr:query')")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") String addressId)
    {
        return success(customerAddrService.selectCustomerAddrByAddressId(addressId));
    }

    /**
     * 查询客户地址管理列表  根据客户id
     */
    @PreAuthorize("@ss.hasPermi('system:addr:query')")
    @GetMapping(value = "addr/{customerId}")
    public AjaxResult getcustomerInfo(@PathVariable("customerId") String customerId)
    {
        return success(customerAddrService.selectCustomerAddrByCustomerId(customerId));
    }

    /**
     * 新增客户地址管理
     */
    @PreAuthorize("@ss.hasPermi('system:addr:add')")
    @Log(title = "客户地址管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerAddr customerAddr)
    {
        return toAjax(customerAddrService.insertCustomerAddr(customerAddr));
    }

    /**
     * 修改客户地址管理
     */
    @PreAuthorize("@ss.hasPermi('system:addr:edit')")
    @Log(title = "客户地址管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerAddr customerAddr)
    {
        return toAjax(customerAddrService.updateCustomerAddr(customerAddr));
    }

    /**
     * 删除客户地址管理
     */
    @PreAuthorize("@ss.hasPermi('system:addr:remove')")
    @Log(title = "客户地址管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable String[] addressIds)
    {
        return toAjax(customerAddrService.deleteCustomerAddrByAddressIds(addressIds));
    }


    @DeleteMapping("/delete")
    public void deleteCustomerAddr(@RequestBody CustomerAddr customerAddr) {
        customerAddrService.deleteCustomerAddr(customerAddr);
    }


    @GetMapping("/getByCustomerIdAndAddressId")
    public AjaxResult getCustomerAddrByCustomerIdAndAddressId(@RequestParam String customerId, @RequestParam String addressId) {
        CustomerAddr customerAddr = customerAddrService.selectCustomerAddrByCustomerIdAndAddressId(customerId, addressId);
        if (customerAddr != null) {
            return success(customerAddr);
        } else {
            return error("未找到对应的客户地址信息");
        }
    }
}
